package cn.enilu.flash.api.controller.park;

import cn.enilu.flash.bean.entity.park.ParkingQuoteRef;
import cn.enilu.flash.bean.view.ParkQuoteRefView;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.park.ParkingQuoteRefService;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.dictmap.CommonDict;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.GunsException;
import cn.enilu.flash.bean.vo.front.Rets;

import cn.enilu.flash.utils.BeanUtil;
import cn.enilu.flash.utils.Maps;
import cn.enilu.flash.utils.ToolUtil;
import cn.enilu.flash.utils.factory.Page;

import cn.enilu.flash.warpper.ParkQuoteRefWarpper;
import cn.enilu.flash.warpper.ParkQuoteWarpper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/quote/ref")
public class ParkingQuoteRefController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ParkingQuoteRefService parkingQuoteRefService;

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public Object list() {
	Page<ParkingQuoteRef> page = new PageFactory<ParkingQuoteRef>().defaultPage();
		page = parkingQuoteRefService.queryPage(page);
		return Rets.success(page);
	}
	@RequestMapping(method = RequestMethod.POST)
	@BussinessLog(value = "编辑车位报价关联", key = "name",dict= CommonDict.class)
	public Object save(@ModelAttribute ParkingQuoteRef parkingQuoteRef){
		if(parkingQuoteRef.getId()==null){
			parkingQuoteRefService.insert(parkingQuoteRef);
		}else {
			parkingQuoteRefService.update(parkingQuoteRef);
		}
		return Rets.success();
	}

	@RequestMapping(value = "saveAll", method = RequestMethod.POST)
	@BussinessLog(value = "编辑车位报价关联", key = "name",dict= CommonDict.class)
	public Object saveAll(@ModelAttribute ParkQuoteRefView parkQuoteRefView){
		parkingQuoteRefService.saveAll(parkQuoteRefView);
		return Rets.success();
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@BussinessLog(value = "删除车位报价关联", key = "id",dict= CommonDict.class)
	public Object remove(Long id){
		if (ToolUtil.isEmpty(id)) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		parkingQuoteRefService.delete(id);
		return Rets.success();
	}

	@RequestMapping(value = "/allSelectors/{communityId}/{parkingId}",method = RequestMethod.GET)
	public Object allSelectors(@PathVariable("communityId") Long communityId, @PathVariable("parkingId") Long parkingId) {
		List list = (List) new ParkQuoteRefWarpper(BeanUtil.objectsToMaps(parkingQuoteRefService.getAllQuotes(communityId, parkingId))).warp();
		return Rets.success(list);
	}

	@RequestMapping(value = "/selectors/{parkingId}",method = RequestMethod.GET)
	public Object selectors(@PathVariable("parkingId") Long parkingId) {
		SearchFilter sf = SearchFilter.build("parkingId", SearchFilter.Operator.EQ, parkingId);
		List list = (List) new ParkQuoteRefWarpper(BeanUtil.objectsToMaps(parkingQuoteRefService.queryAll(sf))).warp();
		return Rets.success(list);
	}
}